package com.note7_5;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;


public class Question2 {
	
	/**
	 * 输入一个文本文件名，把其内容用byte数组的方式输出
	 */
	public static void main(String[] args) {
		InputStream is = null;
		Scanner in = new Scanner(System.in);
		System.out.println("请输入文件位置：");
		String path = in.nextLine();
		try {	//读取文件中的内容
			is = new FileInputStream(path);
			byte[] b = new  byte[is.available()];
			int len = 0;
			while((len = is.read(b)) > -1){
				String s = new String(b,0,len,"GBK");
				System.out.println(s);
			}
		} catch (FileNotFoundException e) {	//文件打开失败
			System.out.println("文件打开失败！（请检查文件位置是否正确）");
			e.printStackTrace();
		} catch (IOException e1) {	
			System.out.println("IO错误");
			e1.printStackTrace();
		} finally{	//关闭输入流
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					System.out.println("关闭输入流失败");
					e.printStackTrace();
				}
			}
		}
	}
}
